package com.openbravo.data.gui;

import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeCellRenderer;
import org.jdesktop.swingx.JXTree;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.LocalRes;
import com.openbravo.data.user.BrowseListener;
import com.openbravo.data.user.I_BrowsableTreeEditableData;

public class JTreeNavigator extends JPanel implements BrowseListener, TreeSelectionListener{

    protected I_BrowsableTreeEditableData m_bd;
    
    public JTreeNavigator(I_BrowsableTreeEditableData bd) {
        this(bd, false);
    }
    
    public JTreeNavigator(I_BrowsableTreeEditableData bd, boolean bTouchable) {
        
        m_bd = bd;
        
        initComponents();
        
        if (bTouchable) {
            jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
            jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        }
       
        m_jTree.setModel(m_bd.getTreeModel());
        m_jTree.addTreeSelectionListener(this);

        m_bd.addBrowseListener(this);
    }
	
    public void setCellRenderer(TreeCellRenderer cellRenderer) {
        m_jTree.setCellRenderer(cellRenderer);
    }
    
    public void updateIndex(int iIndex, int iCounter) {
    } 
    
    
    @Override
	public void valueChanged(TreeSelectionEvent evt) {
    	try {
			m_bd.moveTo(evt.getPath());
		} catch (BasicException e) {
			 MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, LocalRes.getIntString("message.nomove"), e);
             msg.show(this);
		}
		
	}
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        m_jTree = new JXTree();

        setLayout(new java.awt.BorderLayout());

        setPreferredSize(new java.awt.Dimension(200, 2));
        m_jTree.setShowsRootHandles(false);
        m_jTree.setFocusable(false);
        m_jTree.setRequestFocusEnabled(false);
        jScrollPane1.setViewportView(m_jTree);
        m_jTree.setRootVisible(false);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    protected JXTree m_jTree;
    // End of variables declaration//GEN-END:variables
}
